package com.yujistu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.yujistu.model.BaseModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Win on 2015/11/6.
 */
public class BaseListAdapter<T> extends BaseAdapter {

    protected LayoutInflater mInflater;
    protected Context mContext;

    public BaseListAdapter(Context context) {
        this.mContext = context;
        this.mInflater = getLayoutInflater(context);
    }

    protected LayoutInflater getLayoutInflater(Context context) {
        if (mInflater == null) {
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        return mInflater;
    }

    protected ArrayList<T>  mDatas = new ArrayList<>();

    @Override
    public int getCount() {
        if(null != mDatas){
            return mDatas.size();
        }
        return 0;
    }

    @Override
    public T getItem(int i) {
        if (mDatas.size() > i) {
            return mDatas.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void setData(ArrayList<T> data) {
        mDatas = data;
        notifyDataSetChanged();
    }

    public ArrayList<T> getData() {
        return mDatas == null ? (mDatas = new ArrayList<T>()) : mDatas;
    }

    //往前插数据
    public void addDataFront(List<T> data) {
        if (data == null || data.isEmpty()) {
            return;
        }
        List<T> tempData = new ArrayList<T>();
        if (mDatas != null) {
            tempData.addAll(mDatas);
            mDatas.clear();
            mDatas.addAll(data);
            mDatas.addAll(tempData);
        }
        notifyDataSetChanged();
    }

    public void addData(List<T> data) {
        if (mDatas != null && data != null && !data.isEmpty()) {
            mDatas.addAll(data);
        }
        notifyDataSetChanged();
    }

    public void addItem(T obj) {
        if (mDatas != null) {
            mDatas.add(obj);
        }
        notifyDataSetChanged();
    }

    public void addItem(int pos, T obj) {
        if (mDatas != null) {
            mDatas.add(pos, obj);
        }
        notifyDataSetChanged();
    }

    public void removeItem(Object obj) {
        mDatas.remove(obj);
        notifyDataSetChanged();
    }

    public void clear() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
//        if (position < 0){
//            position = 0; // 若列表没有数据，是没有footview/headview的
//        }
        return getRealView(position, convertView, viewGroup);
    }

    protected View getRealView(int position, View convertView, ViewGroup viewGroup) {
        return null;
    }

    public class BaseViewHolder{

        public BaseViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
